/**
 * Created by Administrator on 2017/4/20.
 */

var Room = require('../../game/room').Room;

var Poker = require('./Poker');
var PublicCards = require('./publicCards').PublicCards;
var SubCode = require('../subCode');

exports.PokerRoom = Room.extend({
    _publicCards: null,        // 公牌

    _dealerIndex: 0,           // 庄家坐位
    _dealerLZ: 0,              // 连庄次数

    _curPlay: 0,               // 当前玩家座位号

    init: function(rid, creator, msg) {
        this._super(rid, creator, msg);

        this._publicCards = new PublicCards(this);

        this._dealerIndex = 0;
        this._dealerLZ = 0;
        this._curPlay = 0;
    },

    getPublicCards: function() {
        return this._publicCards;
    },

    getDealerPlayer: function() {
        return this.getPlayerByIndex(this._dealerIndex);
    },

    getDealerIndex: function() {
        return this._dealerIndex;
    },

    getDealerLZ: function() {
        return this._dealerLZ;
    },

    getCurPlay: function() {
        return this._curPlay;
    },

    // 设置当前玩家座位号
    setCurPlay: function (curPlay) {
        this._curPlay = curPlay;
    },

    getCurPlayer: function() {
        return this.getPlayerByIndex(this._curPlay);
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    getInfo: function() {
        var info = this._super();
        info.dealer = this._dealerIndex;
        info.dealerLZ = this._dealerLZ;
        info.curPlay = this._curPlay;
        return info;
    },

    getReconnectInfo: function(rePlayer) {
        var info = this._super(rePlayer);
        info.dealer = this._dealerIndex;
        info.dealerLZ = this._dealerLZ;
        info.curPlay = this._curPlay;
        info.publicCardNum = this._publicCards.getRemainNum();
        return info;
    },

    getSettlementInfo: function() {
        var info = this._super();
        info.dealerIndex = this.getDealerIndex();
        info.dealerLZ = this.getDealerLZ();
        return info;
    },

    onBeforeStartNewRound: function() {
        this._super();
        this._publicCards.gen();

        // 开始回合清空当前玩家
        this._curPlay = 0;
    },

    /******************************************************************
     * 功能接口
     ******************************************************************/
    /**
     * 给玩家发牌
     */
    initPlayersHandCards: function() {
        this.enumPlaying(function(eIndex, ePlayer) {
            ePlayer.initHandCards(this._publicCards.getCards(ePlayer.getInitHandCardNeedNum(), ePlayer.getInitHandCardPreCards()));
        }.bind(this));

        this._sendPublicCardChanged();
    },

    /**
     * 发送公牌数量
     * @private
     */
    _sendPublicCardChanged: function() {
        this.broadcast("onPublicCardChanged", {
            "cardNum": this._publicCards.getRemainNum(),
        });
    },


    //gm接口
    handleGmSetPublicCards: function(player, msg, callback) {
        if (!player.isSuperGM()) {
            callback({code: Code.GM_LEVEL_LIMIT}); return;
        }

        this._publicCards.setRemainCards(msg.cards);
        callback({});
    },
    handleGmSetHandCards: function(player, msg, callback){
        player.setInitHandCardPreCards(msg.cards);
        callback({});

    },
    handleGmGetPublicCards: function(player, msg, callback) {
        if (!player.isSuperGM()) {
            callback({code: Code.GM_LEVEL_LIMIT}); return;
        }

        callback({"cards": this._publicCards.getRemainCards()});
    },
});